home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / System / Sample 2.4 Think C distribution / wstm.c < prev    next >
Text File  |  1990-07-10  |  5KB  |  169 lines

  1. /*______________________________________________________________________
  2.  
  3.     wstm.c - Window State Module
  4.     
  5.     Copyright © 1988, 1989, 1990 Northwestern University.  Permission is granted
  6.     to use this code in your own projects, provided you give credit to both
  7.     John Norstad and Northwestern University in your about box or document.
  8.     
  9.     This module keeps track of window states.
  10.     
  11.     All of the code is placed in its own segment named "wstm".
  12. _____________________________________________________________________*/
  13.  
  14.  
  15. #include "utl.h"
  16. #include "rez.h"
  17. #include "glob.h"
  18. #include "wstm.h"
  19.  
  20. /*______________________________________________________________________
  21.  
  22.     wstm_Init - Initialize Window State.
  23.     
  24.     Entry:    windState = pointer to WindState struct.
  25.                     
  26.     Exit:        window state information initialized.
  27. _____________________________________________________________________*/
  28.  
  29.  
  30. void wstm_Init (WindState *windState)
  31.  
  32. {
  33.     windState->moved = false;;
  34. }
  35.  
  36. /*______________________________________________________________________
  37.  
  38.     wstm_Save - Save Window State.
  39.     
  40.     Entry:    theWindow = window pointer.
  41.                 windState = pointer to WindState struct.
  42.                     
  43.     Exit:        window state information saved in WindState structure.
  44. _____________________________________________________________________*/
  45.  
  46.  
  47. void wstm_Save (WindowPtr theWindow, WindState *windState)
  48.  
  49. {
  50.     if (windState->moved) {
  51.         utl_SaveWindowPos(theWindow, &windState->userState, &windState->zoomed);
  52.     };
  53. }
  54.  
  55. /*______________________________________________________________________
  56.  
  57.     wstm_ComputeStd - Compute a Window's Standard State.
  58.     
  59.     Entry:    theWindow = pointer to window.
  60.     
  61.     Exit:        computed standard state stored in state data block.
  62.     
  63.     See TN 79.
  64. _____________________________________________________________________*/
  65.  
  66.  
  67. void wstm_ComputeStd (WindowPtr theWindow)
  68.  
  69. {
  70.     WindowPeek        w;                    /* window pointer */
  71.     WStateData        **stateData;    /* handle to zoom state data */
  72.     GDHandle            gd;                /* handle to gdevice containing window */
  73.     Rect                zoomRect;        /* rect to zoom to */
  74.     Rect                windRect;        /* window rect in global coords */
  75.     Boolean            hasMB;            /* true if window contains menu bar */
  76.     short                windWidth;        /* width of window */
  77.     
  78.     w = (WindowPeek)theWindow;
  79.     if (!(w->dataHandle && w->spareFlag)) return;
  80.     utl_GetWindGD(theWindow, &gd, &zoomRect, &windRect, &hasMB);
  81.     zoomRect.top += titleBarHeight + zoomSlop + 
  82.         (hasMB ? utl_GetMBarHeight() : 0);
  83.     zoomRect.bottom -= 3;
  84.     stateData = (WStateData**)w->dataHandle;
  85.     windWidth = windRect.right - windRect.left;
  86.     if (zoomRect.left <= windRect.left &&
  87.         windRect.right <= zoomRect.right) {
  88.         zoomRect.left = (**stateData).userState.left;
  89.         zoomRect.right = zoomRect.left + windWidth;
  90.     } else if (windRect.right > zoomRect.right) {
  91.         zoomRect.right -= 3;
  92.         zoomRect.left = zoomRect.right - windWidth;
  93.     } else {
  94.         zoomRect.left += 3;
  95.         zoomRect.right = zoomRect.left + windWidth;
  96.     };
  97.     (**stateData).stdState = zoomRect;
  98. }
  99.  
  100. /*______________________________________________________________________
  101.  
  102.     wstm_ComputeDef - Compute a Window's Default State.
  103.     
  104.     Entry:    theWindow = pointer to window.
  105.     
  106.     Exit:        userState = computed state rectangle (location and size).
  107. _____________________________________________________________________*/
  108.  
  109.  
  110. void wstm_ComputeDef (WindowPtr theWindow, Rect *userState)
  111.  
  112. {
  113.     Point            pos;            /* window position */
  114.     
  115.     *userState = theWindow->portRect;
  116.     utl_StaggerWindow(userState, staggerInitialOffset, staggerOffset, &pos);
  117.     OffsetRect(userState, pos.h, pos.v);
  118. }
  119.  
  120. /*______________________________________________________________________
  121.  
  122.     wstm_Restore - Restore Window State.
  123.     
  124.     Entry:    dlog = true if dialog window, false if regular window.
  125.                 windID = resource ID of window template.
  126.                 wStorage = pointer to window storage, or nil.
  127.                 windState = pointer to saved window state.
  128.                     
  129.     Exit:        function result = pointer to window, positioned and sized.
  130. _____________________________________________________________________*/
  131.  
  132.  
  133. WindowPtr wstm_Restore (Boolean dlog, short windID, Ptr wStorage,
  134.     WindState *windState)
  135.  
  136. {
  137.     WindowPtr        theWindow;            /* pointer to window */
  138.     Rect                userState;            /* user state rectangle */
  139.     
  140.     theWindow = dlog ?
  141.         GetNewDialog(windID, (DialogPeek)wStorage, (WindowPtr)-1) :
  142.         utl_GetNewWindow(windID, wStorage, (WindowPtr)-1);
  143.     if (!windState->moved) {
  144.         wstm_ComputeDef(theWindow, &userState);
  145.         MoveWindow(theWindow, userState.left, userState.top, false);
  146.         SizeWindow(theWindow, userState.right-userState.left,
  147.             userState.bottom-userState.top, true);
  148.     } else {
  149.         utl_RestoreWindowPos(theWindow, &windState->userState, windState->zoomed,
  150.             dragSlop, wstm_ComputeStd, wstm_ComputeDef);
  151.     };
  152.     return theWindow;
  153. }
  154.  
  155. /*______________________________________________________________________
  156.  
  157.     wstm_Mark - Mark a Window Moved or Sized.
  158.     
  159.     Entry:    windState = pointer to saved window state.
  160. _____________________________________________________________________*/
  161.  
  162.  
  163. void wstm_Mark (WindState *windState)
  164.  
  165. {
  166.     windState->moved = true;
  167. };
  168.  
  169.